package models_main

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
	"longmen/server/pkg/db/mysql/models"
	"strings"
)

type OfflineMsg struct {
	ID         int            `gorm:"primary_key"`
	Title      string         `gorm:"column:title"`
	Content    string         `gorm:"column:content"`
	SportId    int8           //1足球 2篮球
	MatchId    int            //赛事ID
	Comp       string         //联赛名
	User       OfflineMsgUser `gorm:"type:json"`
	PushTime   int            // 推送时间
	PushStatus int8           // 推送状态 1-待推送 2-已推送
	JumpType   int8           `gorm:"column:jump"` // 跳转类型 0=打開app 1=跳至官方直播间 2=跳至主播直播间
	Operator   string         // 操作人
	CreateTime int            // 创建时间
}

type OfflineMsgUser []string

func (OfflineMsg) TableName() string {
	return fmt.Sprintf("%soffline_msg", models.Prefix)
}

func (s OfflineMsgUser) Value() (driver.Value, error) {
	if len(s) == 0 {
		return "[]", nil
	}
	return fmt.Sprintf(`["%s"]`, strings.Join(s, `","`)), nil
}

func (s *OfflineMsgUser) Scan(src interface{}) (err error) {
	var skills []string
	switch src.(type) {
	case string:
		err = json.Unmarshal([]byte(src.(string)), &skills)
	case []byte:
		err = json.Unmarshal(src.([]byte), &skills)
	default:
		return errors.New("Incompatible type for Skills")
	}
	if err != nil {
		return
	}
	*s = skills
	return nil
}
